<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Slider extends CI_Controller {
	
	function __construct() {
        parent::__construct();	
		$this->isLogin();	
		$this->path = './foto_slider/';
		$this->load->model('slider_model');
		
	}
	
	function isLogin(){
		$login=$this->session->userdata('isLogin');
		if(! isset($login)||$login != true){
			$this->session->set_flashdata('error_login', 'Untuk Mengakses Admin Anda Harus Login!');
			redirect('login');	
		}
	}

	public function index($offset = 0)
	{
		
		$data['main_view']='slider/main';
		$uri_segment = 3;
		
		if($offset != 0){
			$offset = $this->uri->segment($uri_segment);
		}else{
			$offset=$offset;
		}
		$num_post=10;
		$data['hasil']=$this->slider_model->getData($num_post, $offset);
		$data['offset']=$offset;
		$num_rows = $this->slider_model->count_all_num_rows();
			
		if($num_rows>0){
			// Membuat pagination
			$config['base_url'] = base_url().'admin_slider/index';
			$config['cur_tag_open'] = '<li class=active><a href="#">';
			$config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
			$config['num_tag_open'] = '<li>';
			$config['num_tag_close'] = '</li>';
			$config['prev_link'] = '<';
			$config['prev_tag_open'] = '<li>';
			$config['prev_tag_close'] = '</li>';
			$config['next_link'] = '>';
			$config['next_tag_open'] = '<li>';
			$config['next_tag_close'] = '</li>';
			//buat last
			$config['last_tag_open'] = '<li>';
			$config['last_tag_close'] = '</li>';
			$config['last_link'] = '>>';
			//buat first
			$config['first_tag_open'] = '<li>';
			$config['first_tag_close'] = '</li>';
			$config['first_link'] = '<<';
			$config['total_rows'] = $num_rows;
			$config['per_page'] = $num_post;
			$config['uri_segment'] = $uri_segment;
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
		}
		
		$this->load->view('admin_template',$data);
	}
	
	function view_add(){
		$data['main_view']='slider/view_add';
		$this->load->view('admin_template',$data);
	}
	
	function proses_add(){
		$judul=$this->input->post('judul');
		$konten=$this->input->post('konten');
		$tmp_gambar=$this->input->post('gambar');
		$link=$this->input->post('link');
		$text_link=$this->input->post('text_link');
		if(!empty($_FILES['gambar']['name'])){
			$config = array(
 				'allowed_types'=>'jpg|jpeg|gif|png',
 				'upload_path' => $this->path,
 				'max_size' => 2000);

			$this->load->library('upload',$config);
			$status_upload=$this->upload->do_upload('gambar');
 			$image_data = $this->upload->data();
			//print_r($image_data);
			$nama_gambar=$image_data['file_name'];
		}else{
			$nama_gambar="";
			
		}
		
		if($query=$this->slider_model->insert_data($judul,$nama_gambar,$konten,$link,$text_link)){
			$this->session->set_flashdata('pesan', 'Data Sukses Ditambah');
			redirect('admin_slider');
			
		}
	}
	
	function view_edit($id){
		$data['main_view']='slider/view_edit';
		$data['hasil']=$this->slider_model->get_detail_data($id);
		$this->load->view('admin_template',$data);
	}
	
	function proses_update(){
		$judul=$this->input->post('judul');
		$konten=$this->input->post('konten');
		$link=$this->input->post('link');
		$text_link=$this->input->post('text_link');
		$id=$this->input->post('id');
		$tmp_gambar=$this->input->post('tmp_gambar');
		if(!empty($_FILES['gambar']['name'])){
			$config = array(
 				'allowed_types'=>'jpg|jpeg|gif|png',
 				'upload_path' => $this->path,
 				'max_size' => 2000);

			$this->load->library('upload',$config);
			$status_upload=$this->upload->do_upload('gambar');
 			$image_data = $this->upload->data();
			//print_r($image_data);
			$nama_gambar=$image_data['file_name'];
		}else{
			$nama_gambar=$tmp_gambar;
			
		}
		
		if($query=$this->slider_model->update_data($id,$judul,$nama_gambar,$konten,$link,$text_link)){
			$this->session->set_flashdata('pesan', 'Data Sukses Ditambah');
			redirect('admin_slider');
			
		}
	}
	
	function delete($id){
		$this->slider_model->delete_data($id);
		$this->session->set_flashdata('pesan', 'Data Sukses Dihapus');
		redirect('admin_slider');
	}
}